// pages/checkResource/checkResource.js
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    orgId: '',
    detail: {}
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  openFile(e){
    var dataset = e.currentTarget.dataset;
    wx.downloadFile({
      url: dataset.url,
      success: function (res) {
        var filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toggle("id", options.id);
    this.toggle("orgId", options.orgId);
  },
  refreshDetail: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.detail();
    });
  },
  detail: function() {
    var that = this;
    app.req({
      url: "orgmini/notice/info",
      data: {
        id: that.data.id,
        orgId: that.data.orgId,
        sourceType:1
      },
      success: function(res) {
        if (200 == res.code) {
          var detail = res.data;
          detail.createTimeShow = detail.createTime.substr(5, 11).replace("-", "月").replace(" ", "日 ");
          that.toggle("detail", res.data);
          var content = that.escape2Html(detail.content);
          var rich = content;
          that.toggle('rich', content);
          WxParse.wxParse('rich', 'html', rich, that, 5);
        }
      }
    });
  },
  escape2Html: function (str) {
    var arrEntities = {
      'lt': '<',
      'gt': '>',
      'nbsp': ' ',
      'amp': '&',
      'quot': '"'
    };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
      return arrEntities[t];
    });
  },
  pass: function() {
    this.operation(1);
  },
  noPass: function() {
    this.operation(2);
  },
  operation: function(status) {
    var that = this;
    var data = {
      orgId: that.data.detail.orgId,
      id: that.data.id,
      status: status
    };
    app.req({
      url: "orgmini/manager/audit/orgCheckTask",
      data: data,
      success: function(res) {
        if (200 == res.code) {
          wx.navigateBack();
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.refreshDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshDetail();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})